import { Controller, Get, Param, Post, Body, Put, Delete, Query, Req } from '@nestjs/common';
import { UserService } from './user.service';
import { User as UserModel, Post as PostModel } from '@prisma/client';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { RegUser, UpdateUser, UsersDto, QueryUser } from './dto';
import { Request } from 'express';
@Controller('user')
@ApiTags('用户')
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Get('users')
  @ApiOperation({ summary: '查询所有用户' })
  async getUser(@Query() query: QueryUser): Promise<UsersDto> {
    const { page, pageSize } = query
    let skip = undefined
    let take = undefined
    if (page && pageSize) {
      skip = +page < 1 ? 0 : (page - 1) * pageSize
      take = +pageSize
    }
    return this.userService.users(skip, take)
  }

  @Get('user/:id')
  @ApiOperation({ summary: '查询用户' })
  async getUserById(@Param('id') id: string) {
    return this.userService.user({ id: +id })
  }

  @Get('postByuserId/:id')
  @ApiOperation({ summary: '查询用户帖子' })
  async getPostByUserId(@Param('id') id: string): Promise<PostModel[]> {
    return this.userService.getUserPosts({ id: +id })
  }

  @Post('user')
  @ApiOperation({ summary: '注册用户' })
  async signupUser(@Body() userData: RegUser) {
    return this.userService.createUser(userData)
  }

  @Put('updateUser')
  @ApiOperation({ summary: '更新用户' })
  async updateUser(@Body() userData: UpdateUser) {
    const { id, name } = userData
    return this.userService.updateUser({ where: { id }, data: { name } })
  }

  @Delete('deleteUser/:id')
  @ApiOperation({ summary: '删除用户' })
  async deleteUser(@Param('id') id: number) {
    return this.userService.deleteUser({ id })
  }
}
